package com.bookstore.service.impl;

import com.bookstore.entity.Book;
import com.bookstore.entity.PageBean;
import com.bookstore.entity.User;
import com.bookstore.mapper.BookMapper;
import com.bookstore.mapper.UserMapper;
import com.bookstore.service.BookService;
import com.bookstore.tools.CreateSqlSession;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author Cofe
 * @Date 2022/4/17 8:35
 */
@Service
public class BookServiceImpl implements BookService {
    @Resource
    private BookMapper bm;

    @Override
    public Book findBookById(int bid) {
        return bm.findById(bid);
    }

    @Override
    public List<Book> findAllBook() {

        return bm.findAllBook();
    }


    @Override
    public void saveBook(String book_name, String book_type, String book_intro, int book_number, double book_balance,double book_discount, double book_membalance,String imagname) {
        bm.insertBook(book_name, book_type, book_intro, book_number, book_balance,book_discount,book_membalance,imagname);
    }

    @Override
    public List<Book> findBookTop() {
        return bm.findBookTop();
    }

    @Override
    public PageBean<Book> findBookTopBalance(int currentPage,int pageSize) {
        int begin = (currentPage - 1) * pageSize;
        // 计算查询条目数
        int size = pageSize;
        //5. 查询当前页数据
        List<Book> rows = bm.findBookTopBalance(begin, size);
        //6. 查询总记录数
        System.out.println(rows);
        int totalCount = bm.selectTotalCount();
        //7. 封装PageBean对象
        PageBean<Book> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);
        return pageBean;
    }



    @Override
    public void updateBook(int bid, String book_name, String book_type, String book_intro, int book_number, double book_balance,double book_discount,double book_membalance) {
        bm.updateBook(bid, book_name, book_type, book_intro, book_number, book_balance,book_discount,book_membalance);
    }

    @Override
    public void updateBookNew(int bid, int book_number,  int book_sale) {
        bm.updateBookNew(bid,book_number,book_sale);
    }

    @Override
    public PageBean<Book> selectByPage(int currentPage, int pageSize) {
        int begin = (currentPage - 1) * pageSize;
        // 计算查询条目数
        int size = pageSize;
        //5. 查询当前页数据
        List<Book> rows = bm.selectByPage(begin, size);
        //6. 查询总记录数
        int totalCount = bm.selectTotalCount();
        //7. 封装PageBean对象
        PageBean<Book> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);
        return pageBean;
    }

    @Override
    public PageBean<Book> selectByPageAndCondition(int currentPage, int pageSize, Book book) {
        //计算开始索引
        int begin = (currentPage - 1) * pageSize;
        // 计算查询条目数
        int size = pageSize;
        // 处理brand条件，模糊表达式
        int bookID = book.getBid();
        if (bookID > 0) {
            book.setBid(bookID);
        }
        int bid=book.getBid();
        if (bid > 0) {
            book.setBid(bid);
        }
        String bookName = book.getBook_name();
        if (bookName != null && bookName.length() > 0) {
            book.setBook_name("%" + bookName + "%");
        }
        String bookType = book.getBook_type();
        if (bookType != null && bookType.length() > 0) {
            book.setBook_type("%" + bookType + "%");
        }
        //5. 查询当前页数据
        List<Book> rows = bm.selectByPageAndCondition(begin, size, book);
        //6. 查询总记录数
        int totalCount = bm.selectTotalCountByCondition(book);
        //7. 封装PageBean对象
        PageBean<Book> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);
        return pageBean;
    }

    @Override
    public String deleteBook(int bid) {
        Integer deletebook = bm.deletebook(bid);
        if (deletebook > 0) {
            return "success";
        } else {
            return "error";
        }
    }
}

